package com.xiaoyu.client.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.service.PushService;

public class MyReceiver extends BroadcastReceiver {
    private static final String TAG = "JIGUANG-Example";

    public MyReceiver() {
        super();
    }

    @Override
    public void onReceive(final Context context, Intent intent) {

        // 通知栏点击事件监听
        //通知栏收到消息时触发
        NotificationBarEvent notificationBarEvent = new NotificationBarEvent();
        try {
            Bundle bundle = intent.getExtras();
            if (!JPushInterface.getConnectionState(context)) {
                Intent pushintent = new Intent(context, PushService.class);//启动极光推送的服务
                context.startService(pushintent);
            }
            if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
                String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
                Log.i(TAG, "[MyReceiver] 接收Registration Id : " + regId);

            } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
                Log.i(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
                if (onBroadcastListener != null) onBroadcastListener.onBroadcast(bundle);

            } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
//                if (onBroadcastListener != null) onBroadcastListener.onBroadcast(bundle);
//                String[] strings = new String[]{"android.permission.SYSTEM_ALERT_WINDOW"};
//                Intent myIntent = new Intent("android.intent.action.MAIN");
//                context.sendBroadcast(myIntent);
//                int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
//                Log.i(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);

                notificationBarEvent.receivingNotification(context, bundle.getString(JPushInterface.EXTRA_ALERT));

            } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
//                if (onBroadcastListener != null) onBroadcastListener.onBroadcast(bundle);
//                Log.i(TAG, "[MyReceiver] 用户点击打开了通知");
                Log.i("jx", "onReceive:结果=== " + bundle.getSerializable(JPushInterface.EXTRA_ALERT));
                Log.i("jx", "onReceive:结果到底是啥==== " + bundle.getStringArrayList(JPushInterface.EXTRA_ALERT));

                //TODO:通过状态跳转到不同页面
                notificationBarEvent.openNotification(context, bundle.getString(JPushInterface.EXTRA_ALERT));

            } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
                if (onBroadcastListener != null) onBroadcastListener.onBroadcast(bundle);
                Log.i(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));

            } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
                if (onBroadcastListener != null) onBroadcastListener.onBroadcast(bundle);
                boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);

            } else {
                if (onBroadcastListener != null) onBroadcastListener.onBroadcast(bundle);
            }
        } catch (Exception e) {

        }

    }


    private OnBroadcastListener onBroadcastListener;

    public void setOnBroadcastListener(OnBroadcastListener onBroadcastListener) {
        this.onBroadcastListener = onBroadcastListener;
    }

    public interface OnBroadcastListener {
        void onBroadcast(Bundle bundle);
    }
}